<template>
  <a-card :bordered="false">

    <div class="table-operator" style="text-align: right;">
      <a-button type="primary" class="btn_add" @click="$refs.addForm.add()" icon="plus">新增</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.id"
      id="table-wrapper"
    >

      <span slot="fileName" slot-scope="text,record">
        <a @click="look(record)">预览</a>
        <a-modal :visible="previewVisible" :title="title" :footer="null" @cancel="previewCancel">
          <img alt="example" style="width: 100%" :src="previewImage" />
        </a-modal>
      </span>

      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm class="btn_del" placement="topRight" title="确认删除？" @confirm="() => del(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>

  </a-card>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizgatewaytype/bizGatewayTypeManage'
  import addForm from './addForm'
  import editForm from './editForm'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '型号',
            dataIndex: 'model'
          },
          {
            title: '网关预览',
            scopedSlots: { customRender: 'fileName' }
          },
          {
            title: '名称',
            dataIndex: 'modelName'
          },
          {
            title: '编码',
            dataIndex: 'code'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        previewVisible: false,
        previewImage: '',
        url: window.CONST.VUE_APP_API_BASE_FILE_URL,
        replaceFields: {
          key: 'id'
        },
        title: ''
      }
    },

    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      look (record) {
        this.title = record.modelName + '\xa0\xa0\xa0\xa0\xa0\xa0' + record.model
        this.previewImage = require('@/assets/devices/' + record.code + '.png')
        this.previewVisible = true
      },
      previewCancel () {
        this.previewVisible = false
      }
    }
  }
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
